
//下面是淘宝接口相关js
function AsycOrder(){
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');
		var  opResult=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
		{
		rt=true;
		alert(data[0]['msg']);
		updateMain('Fresh Order Recs OK!','index.php/Order');
		}
		else{alert(data[0]['msg']);}
		};
		
		var url ='index.php/Top/getLatestOrder';
		var datastr ='status=WAIT_SELLER_SEND_GOODS';
	fetchJsonUrl(url,datastr,opResult);	
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}
}

function loginTaobao(){
		var urltest1 = 'http://open.taobao.com/isv/authorize.php?appkey=12081283';
		var urltest2 ='http://container.api.tbsandbox.com/container';
		var urlture = 'http://container.open.taobao.com/container?appkey=12081283';
		var urlcallback = 'http://localhost/Dev/Y/index.php/Top/callback';			
		openWinhtml(urltest1,'获得授权key','authWin',750,250);
		//getSession();
		$('btau').style.display ='block';
		$('aukey').style.display ='block';
}

function getSession(){
var urltest2 = 'http://container.api.tbsandbox.com/container?authcode=';
var urlture = 'http://container.open.taobao.com/container?appkey=12081283';
//var auurl =urlture;
var auurl =urltest2+$('aukey').value;
//alert(auurl);
openWinhtml(auurl,'获得Session','authWin2',600,250);

}

//同步店铺分类，生成htmstr返回
function AscShopCat(){
		var  opResult2=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			//msg(data[0]['msg']);
			//if(data[0]['data'].test("shopcats_list_get_response"))
			if(data[0]['data'].test("sellercats_list_get_response"))
			{
			//var htmlstr = makeShopcatstr(data[0]['data']);
			var htmlstr = makeSellercatstr(data[0]['data']);
			//alert(htmlstr);
			$('shopcattr').innerHTML =htmlstr;
			}
			else
			{
			//var rtjson = JSON.encode(data[0]['data']);
			alert(data[0]['data']);
			}
			}	
		};		
		var url ='index.php/Top/getShopCat';
		var datastr ='';
	fetchJsonUrl(url,datastr,opResult2);
}

function makeShopcatstr(jsonstr){
	var jsobj = JSON.decode(jsonstr);
	var countx = jsobj['shopcats_list_get_response']['shop_cats']['shop_cat'].length ;
	var catx = jsobj['shopcats_list_get_response']['shop_cats']['shop_cat'];
	//alert(jsobj['shopcats_list_get_response']['shop_cats']['shop_cat']);
	var tmpstr ='';
	for(var i=1;i<=countx;i++)
	{
		if(catx[i-1].is_parent = 'false')//只要大类，子类下商品应该包含在大类里面吧？
		{
		tmpstr = tmpstr +'<tr><td>店铺分类</td>'+ '<td id="'+catx[i-1].cid+'">'+catx[i-1].name+'</td>'+'<td>大类编码</td>'+'<td id="'+i+'">'+i+'</td></tr>';
		}
	}
	//alert($type(jsobj));
	return tmpstr;
}

function makeSellercatstr(jsonstr){//根据返回的json分类进行分解为html和sql
	var jsobj = JSON.decode(jsonstr);
	var countx = jsobj['sellercats_list_get_response']['seller_cats']['seller_cat'].length ;
	var catx = jsobj['sellercats_list_get_response']['seller_cats']['seller_cat'];
	//alert(jsobj['shopcats_list_get_response']['shop_cats']['shop_cat']);
	var tmpstr ='';
	for(var i=1;i<=countx;i++)
	{
		if(catx[i-1].parent_cid = '0')//只要大类，子类下商品应该包含在大类里面吧？
		{
		tmpstr = tmpstr +'<tr><td>店铺分类</td>'+ '<td id="'+catx[i-1].cid+'">'+catx[i-1].name+'</td>'+'<td>大类编码</td>'+'<td id="'+i+'">'+i+'</td></tr>';
		}
	}
	//alert($type(jsobj));
	return tmpstr;
}

function istCat(){//把生产的sql执行insert到本地
msg($('sqliststr').innerHTML);	
	
}

//同步分类对应商品信息
function AscCPdt(){
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');	
		var  opResult2=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			var jsobj = JSON.decode(data[0]['data']);
			if((data[0]['data'].test("items_onsale_get_response")) &&($chk(jsobj['items_onsale_get_response']['items'])))
			{
			var htmlstr = makecpdtsql(jsobj);
			//alert(htmlstr);
			$('shopcattr').innerHTML =htmlstr;
			}
			else
			{
			alert(data[0]['msg']+':'+data[0]['data']);
			}
			}	
		};		
		var url ='index.php/Top/getOnsaleItems';
		var datastr ='';
	fetchJsonUrl(url,datastr,opResult2);
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}	
}

function makecpdtsql(jsobj){//处理返回的json产品信息为html和sql	
	var countx = jsobj['items_onsale_get_response']['items']['item'].length ;
	var catx = jsobj['items_onsale_get_response']['items']['item'];
	//alert(jsobj['shopcats_list_get_response']['shop_cats']['shop_cat']);
	var tmpstr ='';
	for(var i=1;i<=countx;i++)
	{
		if(catx[i-1].is_parent = 'false')//只要大类，子类下商品应该包含在大类里面吧？
		{
		tmpstr = tmpstr +'<tr><td>商品iid</td>'+ '<td id="'+i+'">'+catx[i-1].num_iid+'</td>'+'<td>名称::数量::价格::店铺cid</td>'+'<td id="info'+i+'">'+catx[i-1].title+'::'+catx[i-1].num+'::'+catx[i-1].price+'::'+catx[i-1].seller_cids+'</td></tr>';
		}
	}
	//alert($type(jsobj));
	return tmpstr;
}

//存储获得的商品信息到本地数据库
function istPdtInfo(){
	alert('here');
}

//根据post的id显示商品详情
function showItem()
{
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');	
		var  opResult22=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			var jsobj = JSON.decode(data[0]['data']);
			if((data[0]['data'].test("item_get_response")) &&($chk(jsobj['item_get_response']['item'])))
			{
			var htmlstr2 = '<a href="'+jsobj['item_get_response']['item']['detail_url']+'">商品链接地址</a><br/>位置：'+jsobj['item_get_response']['item']['location']['city']+'<br/>数量:'+jsobj['item_get_response']['item']['num']+'<br/>最近修改时间:'+jsobj['item_get_response']['item']['modified']+'<br/>卖家昵称：'+jsobj['item_get_response']['item']['nick']+'<br/>Itemid:'+jsobj['item_get_response']['item']['num_iid']+'<br/>price:：'+jsobj['item_get_response']['item']['price']+'<br/>seller_cids:'+jsobj['item_get_response']['item']['seller_cids']+'<br/>title:：'+jsobj['item_get_response']['item']['title']+'<br/>商品描述：'+jsobj['item_get_response']['item']['desc']+'<br/>外部编码：'+jsobj['item_get_response']['item']['outer_id'];			
			openWinhtml2('显示商品详情',htmlstr2,'ItemDetWin',300,600);
			}
			else
			{
			alert(data[0]['msg']+':'+data[0]['data']);
			}
			}	
		};		
		var url ='index.php/Top/getItem';
		var datastr ='num_iid=1451883741';
	fetchJsonUrl(url,datastr,opResult22);
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}	
}

//更新淘宝上的商品信息
function updateItem(){
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');	
		var  opResult2=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			var jsobj = JSON.decode(data[0]['data']);
			if((data[0]['data'].test("item_update_response")) &&($chk(jsobj['item_update_response']['item'])))
			{
			alert('商品id为'+jsobj['item_update_response']['item']['num_iid']+'的信息，修改成功于：'+jsobj['item_update_response']['item']['modified']);
			}
			else
			{
			alert(data[0]['msg']+':'+data[0]['data']);
			}
			}	
		};		
		var url ='index.php/Top/updateItem';
		var datastr ='';
	fetchJsonUrl(url,datastr,opResult2);
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}	
}

//根据post的id上架商品
function listingItem()
{
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');	
		var  opResult22=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			var jsobj = JSON.decode(data[0]['data']);
			if((data[0]['data'].test("item_update_listing_response")) &&($chk(jsobj['item_update_listing_response']['item'])))
			{
			alert(jsobj['item_update_listing_response']['item']['num_iid']+'上架于'+jsobj['item_update_listing_response']['item']['modified']);			
			}
			else
			{
			alert(data[0]['msg']+':'+data[0]['data']);
			}
			}	
		};		
		var url = 'index.php/Top/listingItem';
		var datastr = "num_iid=1451883741" ;
	fetchJsonUrl(url,datastr,opResult22);
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}
}


//根据post的id下架商品
function delistingItem()
{
if(Cookie.read('is_login_taobao')){
	msg('已登录淘宝');	
		var  opResult21=function (data){
		var rt = false;
		if(data[0]['status']=='ok')
			{rt=true;
			var jsobj = JSON.decode(data[0]['data']);
			if((data[0]['data'].test("item_update_delisting_response")) &&($chk(jsobj['item_update_delisting_response']['item'])))
			{
			alert(jsobj['item_update_delisting_response']['item']['num_iid']+'下架于'+jsobj['item_update_delisting_response']['item']['modified']);			
			}
			else
			{
			alert(data[0]['msg']+':'+data[0]['data']);
			}
			}	
		};		
		var url = 'index.php/Top/delistingItem';
		var datastr = "num_iid=1451883741" ;
	fetchJsonUrl(url,datastr,opResult21);
	}
  else
  	{	msg('Cookie显示未登陆淘宝');
		loginTaobao();
	}
}